package com.lynu.controller;

import com.alibaba.druid.util.StringUtils;
import com.lynu.pojo.User;
import com.lynu.service.UserService;
import com.lynu.utils.JwtHelper;
import com.lynu.utils.Result;
import com.lynu.utils.ResultCodeEnum;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtHelper jwtHelper;
    @PostMapping("/login")
    public Result login(@RequestBody User user){

        Result result=userService.login(user);
        System.out.println("---------result="+result);
        return result;
    }
    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestHeader("token") String token){
        Result result=userService.getUserInfo(token);
        return result;
    }
    @PostMapping("/checkUserName")
    public Result checkUserName(@Param(value = "username") String username){
        Result result=userService.checkUserName(username);
        return result;
    }
    @PostMapping("/regist")
    public Result regist(@RequestBody User user){
        return userService.regist(user);
    }
    @GetMapping("/checkLogin")
    public Result checkLogin(@RequestHeader(value = "token") String token){
        if(StringUtils.isEmpty(token)|| jwtHelper.isExpiration(token)){
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
        return Result.ok(null);

    }

}
